
package kaizone.haining.chaobohui.widget;

import java.io.InputStream;

import kaizone.haining.chaobohui.util.ImageResizer;
import kaizone.haining.chaobohui.util.Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class AnimationImage extends FrameLayout {

    private AnimationDrawable mAnimationDrawable;

    private int mWidth;

    private int mHeight;

    public AnimationImage(Context context) {
        super(context);
        init();
    }

    public AnimationImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initAnim();
    }

    private void initAnim() {
        String[] name = new String[] {
                "anim_1.jpg", "anim_2.jpg", "anim_3.jpg", "anim_4.jpg",
        };
        mAnimationDrawable = new AnimationDrawable();
        for (int i = 0; i < name.length; i++) {
            InputStream in = Utils.getAssetsFile(getContext(), name[i]);
            Bitmap bitmap = ImageResizer.decodeSampledBitmapFromStream(in, 0, 0);
            BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
            mAnimationDrawable.addFrame(drawable, 1500);
        }
        setBackgroundDrawable(mAnimationDrawable);
        mAnimationDrawable.setOneShot(false);
        mAnimationDrawable.start();
    }

}
